package com.acheli.registry.factories;

import com.acheli.ACHeli;
import com.acheli.registry.config.CustomDataBus;
import com.acheli.registry.config.GsonFactoryConfig;
import com.acheli.rideable.entites.RideableEntity;
import com.acheli.rideable.factory.ACHeliEntitiesFactory;
import com.acheli.rideable.factory.gson.GsonEntitiesFactory;
import com.acheli.rideable.factory.instance.UH_1D_SAR;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ACHFactories {
    public static String UH_1D_SAR_FACTORY = "uh_1d_sar";

    public static void initGsonEntities(String PACKNAME, GsonFactoryConfig gsonFactoryConfig) {

        ACHeli.GsonEntitiesFactoryRegisterMap.put(PACKNAME, GsonEntitiesFactory.GsonRideable.class);

//        if (ACHeli.GsonEntitiesConfigRegisterMap.containsKey(PACKNAME)) {
//            ACHeli.GsonEntitiesConfigRegisterMap.remove(PACKNAME);
//        }
        ACHeli.GsonEntitiesConfigRegisterMap.put(PACKNAME, gsonFactoryConfig);
        ACHeli.LOGGER.info(PACKNAME + " VPack has been load/reload.");
    }

    public static void register() {
        ACHeli.EntitiesFactoryRegisterMap.put(UH_1D_SAR_FACTORY, UH_1D_SAR.class);
        CustomDataBus.gsonFactoryConfigMap.forEach(ACHFactories::initGsonEntities);
    }

    public static ACHeliEntitiesFactory<?, ?> getFactoryFromMap(String factoryNameSpace, Object... args) {
        // 假设 factoryClass 是从注册表中获取的 Class 对象
        Class<?> factoryClass = ACHeli.EntitiesFactoryRegisterMap.get(factoryNameSpace);

        try {
            // 获取接受 RideableEntity 参数的构造函数
            Constructor<?> constructor = factoryClass.getConstructor(RideableEntity.class);

            // 使用 rideableEntity 实例化对象
            Object factoryInstance = constructor.newInstance(args);

           return (ACHeliEntitiesFactory<?, ?>) factoryInstance;

        } catch (NoSuchMethodException e) {
            // 处理没有找到对应构造函数的情况
            throw new RuntimeException("构造函数不存在", e);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            // 处理其他实例化异常
            throw new RuntimeException("实例化失败", e);
        } catch (Exception e) {
            ACHeli.LOGGER.warn("错误！！" + e.getMessage());
        }
        return null;
    }

    public static GsonEntitiesFactory.GsonRideable getGsonEntitiesFactory(String factoryName, Object... args) {
        Class<?> factoryClass = ACHeli.GsonEntitiesFactoryRegisterMap.get(factoryName);
        try {
            // 获取接受参数的构造函数
            Constructor<?> constructor = factoryClass.getConstructor(RideableEntity.class, GsonFactoryConfig.class);

            // 使用 rideableEntity 实例化对象
            Object factoryInstance = constructor.newInstance(args);

            return (GsonEntitiesFactory.GsonRideable) factoryInstance;

        } catch (NoSuchMethodException e) {
            // 处理没有找到对应构造函数的情况
            throw new RuntimeException("构造函数不存在", e);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            // 处理其他实例化异常
            throw new RuntimeException("实例化失败", e);
        } catch (Exception e) {
            ACHeli.LOGGER.warn("错误！！" + e.getMessage());
        }
        return null;
    }
}
